﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using GameEngine.Clients.Silverlight.View.Components;
using GameEngine.Model.Data;
using GameEngine.Model.Data.implementation;
using MapEngine;
using MySettlers.Things;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using SLPerlinNoise;

namespace MySettlers.Commands
{
    public class Page_GameNew_In_Command : SimpleCommand
    {
        static IGameVO<int> game;
        override public void Execute(INotification notification)
        {
            if(game==null)
                game = CreateGame();//TODO isto n vai existir, vamos fazer load do level 1
            SendNotification(MyFacade.PAGE_GAMEMAINMENU_IN_COMMAND, game);
        }

        public IGameVO<int> CreateGame()
        {
            //TODO meter isto em config file

            ///Game
            IGameVO<int> game = new GameVO<int>(new GameVOId("1"));

            #region resources
            //game.Resources.Add("maleandfemalefinal.png", new Image() { Source = new BitmapImage(new Uri("/Resources/maleandfemalefinal.png",UriKind.Relative)) });
            //game.Resources.Add("maleandfemalefinal.png", new Image() { Source = new BitmapImage(new Uri("/Resources/tux.png", UriKind.Relative)) });
            //Application.Current.Resources.Add("maleandfemalefinal.png", new Image() { Source = new BitmapImage(new Uri("/Resources/maleandfemalefinal.png", UriKind.Relative)) });
            #endregion

            //Add Views
            #region shapes
            ShapeVO redCube = new ShapeVO(new ShapeVOId("red cube"), new ShapeTypeVOId(ItemUC.ShapeTypeCube))
            { // cube.Id
                Position = new MapEngine.Point3d(),
                //              position  size      color
                TypeModifiers = "0,0,7.5, 20,20,15, 255,0,0,255" //TODO Cube.Color = Red).getModifiers
            };

            ShapeVO yellowCube = new ShapeVO(new ShapeVOId("yellow cube"), new ShapeTypeVOId(ItemUC.ShapeTypeCube))
            { // cube.Id
                Position = new MapEngine.Point3d(),
                TypeModifiers = "0,0,1, 30,30,1, 255,255,0,255" //TODO Cube.Color = Red).getModifiers
            };

            ShapeVO greenSphere = new ShapeVO(new ShapeVOId("green sphere"), new ShapeTypeVOId(ItemUC.ShapeTypeElipse))
            { // cube.Id
                Position = new MapEngine.Point3d(),
                TypeModifiers = "0,0,0,5,5,0,255,0,255" //TODO Cube.Color = Red).getModifiers
            };

            ShapeVO brownCube = new ShapeVO(new ShapeVOId("brown cube"), new ShapeTypeVOId(ItemUC.ShapeTypeCube))
            { // cube.Id
                Position = new MapEngine.Point3d(),
                TypeModifiers = "0,0,2.5,5,5,10,139,69,19,255" //TODO Cube.Color = Red).getModifiers
            };

            ShapeVO arvoreGreenSphere = new ShapeVO(new ShapeVOId("green sphere"), new ShapeTypeVOId(ItemUC.ShapeTypeElipse))
            { // cube.Id
                Position = new MapEngine.Point3d(new Point(), 2),
                TypeModifiers = "0,0,15,20,20,0,255,0,150" //TODO Cube.Color = Red).getModifiers
            };

            Point3d malefemalescale = new Point3d(new Point(0.2f, 0.2f), 0.2f);
            Point3d malefemalescaleinv = new Point3d(new Point(-0.2f, 0.2f), 0.2f);
            string malefemalevelocity = "3";

            #endregion

            #region Views

            ViewVO<int> malePeasantView = new ViewVO<int>(new ViewVOId<int>(MyModel.Instance.CreateNewId()));

            //predadorView.AddState(workSouthState);
            //predadorView.AddShapeToState(walkSouthState, maleWorkSouth);
            //predadorView.AddState(workEastState);
            //predadorView.AddShapeToState(workEastState, maleWorkEast);
            //predadorView.AddState(workWestState);
            //predadorView.AddShapeToState(workWestState, maleWorkWest);
            //predadorView.AddState(workNorthState);
            //predadorView.AddShapeToState(workNorthState, maleWorkNorth);
            ViewVO<int> houseView = new ViewVO<int>(new ViewVOId<int>(MyModel.Instance.CreateNewId()));
            houseView.Shapes[IViewVOConstants.DefaultState].Add(redCube.Id, redCube);

            ViewVO<int> fieldView = new ViewVO<int>(new ViewVOId<int>(MyModel.Instance.CreateNewId()));
            fieldView.Shapes[IViewVOConstants.DefaultState].Add(yellowCube.Id, yellowCube);

            ViewVO<int> grassView = new ViewVO<int>(new ViewVOId<int>(MyModel.Instance.CreateNewId()));
            grassView.Shapes[IViewVOConstants.DefaultState].Add(greenSphere.Id, greenSphere);

            ViewVO<int> treeView = new ViewVO<int>(new ViewVOId<int>(MyModel.Instance.CreateNewId()));
            treeView.Shapes[IViewVOConstants.DefaultState].Add(brownCube.Id, brownCube);
            treeView.Shapes[IViewVOConstants.DefaultState].Add(arvoreGreenSphere.Id, arvoreGreenSphere);

            game.Views.Add(malePeasantView);
            game.Views.Add(houseView);
            game.Views.Add(fieldView);
            game.Views.Add(grassView);
            game.Views.Add(treeView);
            #endregion

            #region Types
            //Add ThingTypes
            PeasantTypeVO peasantMaleTypeVO = new PeasantTypeVO(MyFacade.Instance.peasantMaleTypeId, malePeasantView.Id)
            {
                Velocity = 0.1f

            };
            game.ThingTypes.Add(peasantMaleTypeVO);

            ThingTypeVO<int> houseTypeVO = new BuildingTypeVO(MyFacade.Instance.houseTypeId) {
                ViewId = houseView.Id
            };
            game.ThingTypes.Add(houseTypeVO);

            ThingTypeVO<int> fieldTypeVO = new ThingTypeVO<int>(MyFacade.Instance.fieldTypeId) { 
                ViewId = fieldView.Id
            };
            game.ThingTypes.Add(fieldTypeVO);

            //PeasantTypeVO peasantFemaleTypeVO = new PeasantTypeVO(peasantFemaleType, fieldView.Id)
            //{
            //    Velocity = 0.1f
            //};
            //game.ThingTypes.Add(peasantFemaleTypeVO.Id, peasantFemaleTypeVO);

            ThingTypeVO<int> grassType = new ThingTypeVO<int>(MyFacade.Instance.grassTypeId)
            {
                ViewId = grassView.Id
            };
            game.ThingTypes.Add(grassType);

            ThingTypeVO<int> treeType = new ThingTypeVO<int>(MyFacade.Instance.treeTypeId)
            {
                ViewId = treeView.Id
            };
            game.ThingTypes.Add( treeType);
            #endregion

            /// Level
            ILevelVO<int> level = new LevelVO<int>(new LevelVOId("MyLevel"));

            /// World
            ///     Map
            MapVO<int> map = new MapVO<int>("MyMap");
            map.CreateTiles(new Point(150, 150));
            PerlinNoise3D.GenerateHeightsFromPerlin(map.Tiles,new Point(0, 0), 0.01f, 1, 1, 1, 1, game.MinHeight, game.MaxHeight);
            //map.Tiles[5][5].Height = 10;
            map.Camera.Position = new Point(75, 75);
            map.Camera.Scale = 2.25f;

            IWorldVO<int> MyWorld = new WorldVO<int>(new WorldVOId("MyWorld"))
            {
                Map = map,
            };

            #region Items
            ///     Items

            //world.Items.Add(new PeasantVO(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), peasantMaleTypeVO.Id)
            //{
            //    Position = new MapEngine.Point3d(new Point(75, 76), 0),
            //    State = walkEastState
            //});
            //world.Items.Add(new PeasantVO(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), peasantMaleTypeVO.Id)
            //{
            //    Position = new MapEngine.Point3d(new Point(75, 77), 0),
            //    State = walkWestState
            //});
            //world.Items.Add(new PeasantVO(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), peasantMaleTypeVO.Id)
            //{
            //    Position = new MapEngine.Point3d(new Point(75, 78), 0),
            //    State = walkNorthState
            //});
            //world.Items.Add(new PeasantVO(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), peasantMaleTypeVO.Id)
            //{
            //    Position = new MapEngine.Point3d(new Point(75, 79), 0),
            //    State = walkSouthState
            //});

            //erva
            //world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), grassType.Id)
            //{
            //    Position = new MapEngine.Point3d(new Point(80, 80), 0),
            //});

            //arvore
            /*world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
            {
                Position = new MapEngine.Point3d(new Point(75, 74), 0),
            });*/
            //world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
            //{
            //    Position = new MapEngine.Point3d(new Point(50, 50), 0),
            //});
            //world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
            //{
            //    Position = new MapEngine.Point3d(new Point(50, 100), 0),
            //});
            //world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
            //{
            //    Position = new MapEngine.Point3d(new Point(100, 50), 0),
            //});
            //world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
            //{
            //    Position = new MapEngine.Point3d(new Point(100, 100), 0),
            //});
            //world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
            //{
            //    Position = new MapEngine.Point3d(new Point(25, 75), 0),
            //});
            //world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
            //{
            //    Position = new MapEngine.Point3d(new Point(75, 25), 0),
            //});
            //world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
            //{
            //    Position = new MapEngine.Point3d(new Point(125, 75), 0),
            //});
            //world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
            //{
            //    Position = new MapEngine.Point3d(new Point(75, 125), 0),
            //});
            #endregion

            level.Worlds.Add(MyWorld);

            /// World
            ///     Map
            MapVO<int> menuMap = new MapVO<int>("MenuMap");
            menuMap.CreateTiles(new Point(3, 3));
            //menuMap.GenerateHeightsFromPerlin(new Point(0, 0), 0.01f, 1, 1, 1, 1);
            //map.Tiles[5][5].Height = 10;
            menuMap.Camera.Position = new Point(1, 1);
            menuMap.Camera.Scale = 1f;

            IWorldVO<int> MenuWorld = new WorldVO<int>(new WorldVOId("MenuWorld"))
            {
                Map = menuMap,
            };

            #region Items

            #endregion

            level.Worlds.Add(MenuWorld);

            game.Levels.Add(level);
            game.LevelOrder.Add(0, level.Id);
            return game;
        }
    }

    public class Page_GameNew_Out_Command : SimpleCommand
    {
        override public void Execute(INotification notification)
        {
            
        }
    }
}
